#!/bin/bash
#$ -V
#$ -cwd
#$ -N xingulation_noname
#$ -M arielgue@post.tau.ac.il
#$ -m e
#$ -j y
#$ -l lilach,h_rt=36:00:00
#$ -q java.lilach.q

set DATE=`date '+%Y.%b.%d-%H_%M_%S_%N_%Z'`

#$ -v maxpopulationsize=1000
#$ -v numberofgenes=1000 
#$ -v globalmutationrate=0.01 
#$ -v standardselectionmodifier=0.1 
#$ -v predationeffectfraction=0.0
#$ -v runlength=1000.0
#$ -v outputfile=-
#$ -v inputfile=-
#$ -v jobname=$JOB_NAME 
#$ -v migrationfitnesscost=0.5 
#$ -v outxingfitnesscost=0.8 
#$ -v heterozygotecoefficient=0.5 
#$ -v numberofdemes=2 
#$ -v demecarryingcapacity=100 
#$ -v dememaxpopulationperiod=500 
#$ -v numberofchildren=20 
#$ -v meanoutxingfraction=0.5 
#$ -v faoorganismsstartingfrequency=0.0 
#$ -v weatherchangeindicator=0



# edit batch file. NOTE: all the following parameters should also be in the batch/batch_params.bsh (template) file
 
sed s/%maxpopulationsize%/${maxpopulationsize}/ ~/workspace/xingulation/batch/batch_params.bsh | sed s/%numberofgenes%/${numberofgenes}/ | sed s/%globalmutationrate%/${globalmutationrate}/ | sed s/%standardselectionmodifier%/${standardselectionmodifier}/ | sed s/%predationeffectfraction%/${predationeffectfraction}/ | sed s/%runlength%/${runlength}/ | sed s/%outputfile%/$JOB_NAME.$DATE/ | sed s/%inputfile%/${inputfile}/ | sed s/%jobdate%/$DATE/ | sed s/%jobname%/$JOB_NAME/ | sed s/%migrationfitnesscost%/${migrationfitnesscost}/ | sed s/%outxingfitnesscost%/${outxingfitnesscost}/ | sed s/%heterozygotecoefficient%/${heterozygotecoefficient}/ | sed s/%numberofdemes%/${numberofdemes}/ | sed s/%demecarryingcapacity%/${demecarryingcapacity}/ | sed s/%dememaxpopulationperiod%/${dememaxpopulationperiod}/ | sed s/%numberofchildren%/${numberofchildren}/ | sed s/%meanoutxingfraction%/${meanoutxingfraction}/ | sed s/%faoorganismsstartingfrequency%/${faoorganismsstartingfrequency}/ | sed s/%weatherchangeindicator%/${weatherchangeindicator}/ > ~/workspace/xingulation/batch/$JOB_NAME.bsh 

rm -r -f ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs
mkdir ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs/
cp ~/workspace/xingulation/xingulation.rs/*.xml ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs/.
cp ~/workspace/xingulation/xingulation.rs/model.score ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs/.

# edit repast file outputter config to change the output file names
sed s/fao\.csv/$JOB_NAME\\/$JOB_NAME.fao.$DATE.csv/g ~/workspace/xingulation/xingulation.rs/faoOutputter.xml > ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs/faoOutputter.xml

sed s/uni\.csv/$JOB_NAME\\/$JOB_NAME.fams.$DATE.csv/g ~/workspace/xingulation/xingulation.rs/uniOutputter.xml > ~/workspace/xingulation/xingulation.rs/$JOB_NAME.rs/uniOutputter.xml

#edit the ant file to use the scenario file from the last paragraph
sed s/xingulation\.rs/xingulation.rs\\/$JOB_NAME.rs/g ~/workspace/xingulation/ant/batch_run_xingulation.xml > ~/workspace/xingulation/ant/batch_run_xingulation.$JOB_NAME.xml

ant -f ~/workspace/xingulation/ant/batch_run_xingulation.$JOB_NAME.xml -Dbatchfile="batch/$JOB_NAME.bsh"

